package lancooControl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by Administrator on 2016/10/10.
 * 定义获取系统时间的类
 */
public class GetSystemTime {
    static Calendar cal;

    static String year;

    static String month;

    static String day;

    static String hour;

    static String minute;

    static String second;

    static String my_time_1;

    static String my_time_2;


    public static String getSystemTime(){

        cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));

        year = String.valueOf(cal.get(Calendar.YEAR));
//        month = String.valueOf(cal.get(Calendar.MONTH)+1);
        month = Control.autoGenericCode(String.valueOf(cal.get(Calendar.MONTH)+1),2);
//        day = String.valueOf(cal.get(Calendar.DATE));
        day = Control.autoGenericCode(String.valueOf(cal.get(Calendar.DATE)),2);

        if (cal.get(Calendar.AM_PM) == 0) {

            hour = String.valueOf(cal.get(Calendar.HOUR));

        }else {

            hour = String.valueOf(cal.get(Calendar.HOUR) + 12);
        }

//        minute = String.valueOf(cal.get(Calendar.MINUTE));
            minute = Control.autoGenericCode(String.valueOf(cal.get(Calendar.MINUTE)),2);

//            second = String.valueOf(cal.get(Calendar.SECOND));
            second = Control.autoGenericCode(String.valueOf(cal.get(Calendar.SECOND)),2);




        my_time_1 = year + "-" + month + "-" + day;

        my_time_2 = hour + ":" + minute + ":" + second;

        System.out.println("====AnswerTime=======AnswerTime========AnswerTime====="+my_time_1+" "+my_time_2);

        return my_time_1+" "+my_time_2;
    }


    public static int getTimeLong(String startTime,String stopTime){

        int i_TimeLong = 0;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try
        {
            Date d1 = df.parse(startTime);//"2006-05-26 12:00:00"
            Date d2 = df.parse(stopTime);//"2006-07-02 11:20:00"
            long diff = d2.getTime() - d1.getTime();//这样得到的差值是微秒级别

//            long days = diff / (1000 * 60 * 60 * 24);
//            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
//            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
//
//            System.out.println(""+days+"天"+hours+"小时"+minutes+"分");
//
//            str_TimeLong = ""+days+"天"+hours+"小时"+minutes+"分";

            i_TimeLong = (int)diff/1000;
        }
        catch (Exception e)
        {
        }

        System.out.println("====445533=====i_TimeLong============"+i_TimeLong);
        return i_TimeLong;
    }
}
